package com.jams.goodox.activity.serve.enterprise;

import android.content.DialogInterface;
import android.content.Intent;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.listen.DialogActionListener;
import com.jams.goodox.utils.DeviceUtils;
import com.jams.goodox.utils.EditTextUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.PromptBuilderDialog;

import butterknife.Bind;

import static com.jams.goodox.activity.serve.enterprise.ChooseListForProjectFinancialActivity.CHOOSE_FINANCIAL;
import static com.jams.goodox.activity.serve.enterprise.ChooseListForProjectFinancialActivity.TITLE_FINANCIAL;


/**
 * 项目名称,项目概述的编辑页面
 */
public class ProjectNameAndSummarizeActivity extends BaseActivity {


    @Bind(R.id.et_comment)
    EditText etComment;
    String tittle;
    String getContent;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_project_name_and_summarize;
    }

    @Override
    protected void init() {
        Intent intent = getIntent();
        tittle = StringUtils.newString(intent.getStringExtra(TITLE_FINANCIAL));
        int limitLength = "项目名称".equals(tittle) ? 20 : 1000;
        String hint = "项目名称".equals(tittle) ? getString(R.string.project_name): getString(R.string.project_summarized);
        etComment.setHint(hint);
        etComment.setFilters(new InputFilter[]{new InputFilter.LengthFilter(limitLength)});
        getContent = StringUtils.newString(intent.getStringExtra("CONTENT"));
        if (!TextUtils.isEmpty(getContent)) {
            etComment.setText(getContent);
        }

        StyleUtil.titleBothTextView(this, tittle, "取消", "发送", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch (v.getId()) {
                    case R.id.ler_back:
                        checkPageData();
                        break;
                    case R.id.ler_save:
                        send();
                        break;
                }

            }
        });

    }

    /**
     * 发送数据
     */
    private void send() {
        String content = etComment.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            ToastUtil.show(mContext, "请输入" + tittle);
            return;
        }
        Intent intent = new Intent();
        intent.putExtra(CHOOSE_FINANCIAL, content);
        setResult(100, intent);
        finish();
    }
    /**
        退出时,判断是否有数据
     */
    private void checkPageData() {
        DeviceUtils.closeKeyBoard(this);
        if (!EditTextUtils.editTextIsEmpty(etComment)) {
            new PromptBuilderDialog(this)
                    .setTitle(R.string.tips)
                    .setContent(R.string.publish_drop_tips)
                    .setPositive(R.string.confirm)
                    .setNegative(R.string.cancel)
                    .setCancelAble(true)
                    .setActionClickListener(new DialogActionListener() {
                        @Override
                        public void cancel(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }

                        @Override
                        public void confirm(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            finish();
                        }
                    })
                    .show();
        } else {
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        checkPageData();

    }
}
